<?php

/**
 * IndexController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';


class IndexController extends Zend_Controller_Action {
	
	protected $_form;
	protected $_historyTable;
	private $_dictTable;
	
	public function init() {
		ZendX_JQuery::enableView($this->view);
		$this->dictTable = new Default_Model_DbTable_DictionaryTable();
	}
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$this->view->form = $this->getForm();
		$this->view->headTitle("Your Dictionaries");
			    if (Zend_Auth::getInstance()->hasIdentity()) {
		    	// Identity exists; get it
		    	$identity = Zend_Auth::getInstance()->getIdentity();
		}		
		$this->view->histories = $this->getHistoryTable()->history($this->getUserId(), 30);
		$this->view->dicts = $this->dictTable->fetchAll();
	}
	
	public function getHistoryTable() {
		if (null === $this->_historyTable) {
			$this->_historyTable = new Default_Model_DbTable_HistoryTable();
		}
		return $this->_historyTable;
	}
	
	public function getForm() {
		if (null === $this->_form) {
			$this->_form = new Default_Form_Query();
		}
		
		return $this->_form;
	}
	
	public function getUserId() {
		if (Zend_Auth::getInstance()->hasIdentity()) {
			return $_SESSION["user_info"]["user_id"];
			//
		} else {
			return Default_Model_DbTable_UserTable::ANONYMOUSE_USER_ID;
		}
	}
	
	public function queryAction() {
		/* @var $request Zend_Controller_Request_Http */
		$request = $this->getRequest ();
		if (! $request->isPost ()) {
			$this->_forward ( "index" );
		}
		$form = $this->getForm ();
		$this->view->form = $this->getForm ();
		
		// save as history
		if ($form->isValid ( $request->getPost () )) {
			$history = $this->getHistoryTable();
			$word =  $form->getValue("query");
			$now = date('Y-m-d H:i:s');
			$data = array ('user_id' => $this->getUserId(), 'word' => $word, 'create_date' => $now, 'remote_ip' => $_SERVER['REMOTE_ADDR']);
			$history->insert( $data );
		} else {
		
		}
		
		// process Roots
		if ($form->isValid ( $request->getPost () )) {
			$word =  $form->getValue("query");
			$repos = new Default_Model_WordRootRepository(APPLICATION_PATH . "/models/word_root.xml");
			$this->view->prefixes =  $repos->prefixes($word);
			$this->view->roots =  $repos->roots($word);
			$this->view->suffixes =  $repos->suffixes($word);
		}
		
		// display history
		$this->view->histories = $this->getHistoryTable()->history($this->getUserId(), 30);
		$this->view->dicts = $this->dictTable->fetchAll();
		
		$this->render ( "index" );
	}
	
	/**
	 * marked or unmrked $word as favorite
	 * @param string $word the word want to mark or unmark
	 * @param bool #marked true : mark as favorite; false : unmark favorite. 
	 */
	public function favoriteAction() {
		//$debug = var_export($this, true);
		/* @var $layout Zend_Layout */
		$word = trim($this->_getParam('word', null));
		$marked = trim($this->_getParam('marked', 'true'));
		$layout = $this->view->layout();
		$layout->disableLayout();
		// Check if 'redirector' helper is registered with the broker:
		// the 2 lines above are very important. this action would return html tags from the layout and will look for a phtml file. we disable the layout and suppress auto-rendering of the phtml view files SO that our JSON will be echoed properly to the Javascript…


		/* @var $viewRenderer Zend_Controller_Action_Helper_ViewRenderer */
		$viewRenderer = $this->_helper->getHelper("viewRenderer"); 	
		$viewRenderer->setNoRender(true);
		
		if ($marked == 'true') {
			$this->getHistoryTable()->markFavorite($this->getUserId(), $word, true); 
		} else {
			$this->getHistoryTable()->markFavorite($this->getUserId(), $word, false); 
		} 
		
		//$this->_helper->getHelper("JQuery");
	}

}

